package com.etc.dao.impl;

import com.etc.dao.StudentDao;
import com.etc.model.Student;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private SqlSessionFactory sqlSessionFactory;

    public StudentDaoImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
    public Integer StudentInsert(List<Student> students) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Integer result = null;
        for (Student student : students) {
            result = sqlSession.insert("StudentInsert", student);
            if (result == 1){
                System.out.println("成功添加数据"+student.toString());
            }
            sqlSession.commit();
        }
        sqlSession.close();

        return result;
    }

    public Student studentById(String sid) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Student studentById = sqlSession.selectOne("studentById", sid);
        return studentById;

    }

}
